/**
 * Created by Administrator on 2018/5/30.
 */
/**
 * Created by Administrator on 2018/1/13.
 */
/**
 * Created by Administrator on 2017/12/2.
 */
define(function(require, exports, module) {
    var $=require('zepto');
    api=require('api');
    cn=require('conment');
    module.exports = {
        int:{
            login:sessionStorage.getItem('testKey'),
            codes:'',
            indexCodes:'',
        },
        islogin:function () {
            const _this=this;
            if(_this.int.login) return false
            cn.Ajax(api.isLogin,{
            },function(d){
                if(d.flag==1){
                    sessionStorage.setItem('testKey',true);
                    _this.int.login=true;
                }else{
                    _this.int.login=false;
                }
            },'post')
        },
        indexMarket:function () {
            const _this=this;
            cn.Ajax(api.indexMarket,{
                codes :'000001,399001,399006'
            },function(d){
                var water=new Array();
                var list=d;
                $.each(list,function(i,l,array){
                    var tradePrice=parseFloat(l.tradePrice),preClosePx=parseFloat(l.preClosePx);
                    _this.int.indexCodes+=(l.code+',');
                    water.push('<div class="red" id="indexMar'+l.code+'">' +
                        '<p class="market-name">'+l.stockName+'</p> <h3 class="market-code pri'+l.code+'">'+l.tradePrice+'</h3> ' +
                        '<div class="market-amount"><span class="tra_pre'+l.code+'">'+(tradePrice-preClosePx).toFixed(2)+'</span><span class="f_tra_pre'+l.code+'">'+(((tradePrice-preClosePx)/preClosePx)*100).toFixed(2)+'%</span></div> ' +
                        '</div>');
                });
                $('#indexMarket').append(water.join(''));
                _this.timeIndex_market()
            },'post')
        },
        timeIndex_market:function () {
            const _this=this;
            cn.Ajax(api.indexMarket,{
                codes : _this.int.indexCodes,
            },function(d){
                var list=d;
                $.each(list,function(i,l,array){
                    var tradePrice=parseFloat(l.tradePrice),preClosePx=parseFloat(l.preClosePx);
                    $('.pri'+l.code).text(l.tradePrice);
                    $('.tra_pre'+l.code).text((tradePrice-preClosePx).toFixed())
                    $('.f_tra_pre'+l.code).text((((tradePrice-preClosePx)/preClosePx)*100).toFixed(2)+'%');
                    if(tradePrice>=preClosePx){
                        $('#indexMar'+l.code).addClass('red').removeClass('green');
                    }else{
                        $('#indexMar'+l.code).addClass('green').removeClass('red');
                    }
                });
                setTimeout(function () {
                    _this.timeIndex_market()
                },3000)

            },'post')
        },
        hotList:function () {
            const _this=this;
            cn.Ajax(api.url_stock.moreMarket,{
                codes :'000790,002264,600270,601366,002927,000852,000858,603136,600721,002677'
            },function(d){
                if(d.code==1){
                    var water=new Array();
                    const list=d.data;
                    $.each(list,function(i,l,array){
                        var tradePrice=parseFloat(l.tradePrice),preClosePx=parseFloat(l.preClosePx);
                        _this.int.codes+=(l.code+',');
                        water.push('<li class="'+(tradePrice>=preClosePx?'red':'green')+' list'+l.code+'" data-code="'+l.code+'">' +
                            '<div><p class="hot-stock-name">'+l.stockName+'</p><span class="hot-stock-code">'+l.code+'</span> </div> ' +
                            '<div class="price price'+l.code+'">'+l.tradePrice+'</div> ' +
                            '<div class="zfb zfb'+l.code+'">'+(((tradePrice-preClosePx)/preClosePx)*100).toFixed(2)+'%</div> </li>');
                    });
                    $('#hotMarketList').append(water.join(''));
                    $('#hotMarketList li').on('click',function () {
                        window.location.href='/view/astock_market.html?code='+$(this).data('code')
                    });
                    _this.timeOut()
                }else{
                    cn.alert(d.msg)
                }
            },'post')
        },
        timeOut:function () {
            const _this=this;
            cn.Ajax(api.url_stock.moreMarket,{
                codes : _this.int.codes,
            },function(d){
                if(d.code==1){
                    var list=d.data;
                    $.each(list,function(i,l,array){
                        var tradePrice=parseFloat(l.tradePrice),preClosePx=parseFloat(l.preClosePx);
                        $('.price'+l.code).text(l.tradePrice);
                        $('.zfb'+l.code).text((((tradePrice-preClosePx)/preClosePx)*100).toFixed(2)+'%');
                        if(tradePrice>=preClosePx){
                            $('.list'+l.code).addClass('red').removeClass('green');
                        }else{
                            $('.list'+l.code).addClass('green').removeClass('red');
                        }
                    });
                    setTimeout(function () {
                        _this.timeOut()
                    },3000)
                }else{
                    cn.alert(d.msg)
                }
            },'post')
        },
        init:function(){
            const _this=this;
            _this.hotList();
            _this.indexMarket();
            _this.islogin();
            $('#foot_hot_nav .click_login').on('click',function () {
                var href=$(this).data('href');
                if(_this.int.login){
                    window.location.href='/view/'+href+'.html'
                }else{
                    window.location.href='/view/login.html'
                }
            })
        },

    }
});